From f8c99bf0cf09e2c67d291257b1ef3ca3478e1692 Mon Sep 17 00:00:00 2001 From: robertl Date: Thu, 19 Nov 2009 03:56:03 +0000 Subject: [PATCH] Add serial port detection to Windows. --- gui/serial_win.cpp | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/gui/serial_win.cpp b/gui/serial_win.cpp index 02a1200b4..1d564de71 100644 --- a/gui/serial_win.cpp +++ b/gui/serial_win.cpp @@ -1,4 +1,4 @@ -// $Id: serial_win.cpp,v 1.1 2009/09/02 19:05:27 robertl Exp $ +// $Id: serial_win.cpp,v 1.2 2009/11/19 03:56:03 robertl Exp $ //------------------------------------------------------------------------ // // Copyright (C) 2009 S. Khai Mong . @@ -21,6 +21,8 @@ #include "mainwindow.h" +#if 0 // Does not require Windows 2000 + static const char *deviceNames[] = { "com1:", "com2:", @@ -35,3 +37,35 @@ void MainWindow::osLoadDeviceNameCombos(QComboBox *box) box->addItem(deviceNames[i]); } } + +#else // This code assumes Windows 2000 or later + +// Uses QueryDosDevice(), Minimum supported: Windows 2000 Professional/Server +#include +#include + +void MainWindow::osLoadDeviceNameCombos(QComboBox *box) +{ + char DevList[64*1024-1]; // a single byte more, and certain versions of windows + // always return QueryDosDevice()==0 && GetLastError()==ERROR_MORE_DATA. + // see http://support.microsoft.com/kb/931305 + // Get a list of all existing MS-DOS device names. Stores one or more asciiz strings followed by an extra null. + DWORD res = QueryDosDeviceA(NULL, DevList, sizeof(DevList)); + if (res == 0) + { + DWORD err = GetLastError(); // could check for ERROR_INSUFFICIENT_BUFFER, and retry with a larger buffer. + // but DevList is already at the maximum size it can be without running into kb 931305. + // FIXME: This shold be a QMessageBox::warning() - RJL + // fprintf(stderr,"QueryDosDevice() failed with %d. GetLastError()==%d.\n", res, err); + return; + } + + for (char *p=DevList; *p;) { + int len = strlen(p); + if (strncmp(p,"COM",3)==0) + box->addItem((PCHAR)p); + p += len+1; // +1 to also skip the null character of each string + } +} + +#endif -- 2.30.2